<?php

defined('__EAPI_BASE') or define('__EAPI_BASE', dirname(__FILE__));

require_once 'api.database.php';

class EAPIUser extends EAPIDatabase {

	private $EAPIUser;

	public function __Construct($userName, $apiUser, $apiKey, $debug=false){

		try{
			//if any user data does not exist
			if(empty($userName) || empty($apiUser) || !is_numeric($apiUser) || empty($apiKey)){
				
				//throw a user data exception
				throw new EAPIException("User Details Not Defined.",603.1);

			}

			//construct the parent class
			parent::__Construct();

			//create the EAPIxml class to set credentials
			$xml = new EAPIxml($this->apicore);

			//set the user credentials and get the characterID
			$charID = $xml->setCredentials($userName,$apiUser,$apiKey);

			//check to see that the charID is not empty
			if(empty($charID)){
				
				//if the charID does not exist, throw an exception
				throw new EAPIException('CharacterID Not Defined.',603.2);

			}
			
			//set the EAPIUser object credentials
			$this->EAPIUser = array("__User" => $userName,
									"__UserID" => $charID,
									"__APIUser" => $apiUser,
									"__APIKey" => $apiKey
									);

									
			if(empty($this->EAPIUser)){
				
				throw new EAPIException('User Details Not Defined',603.3);
				
			}

			//if debugging is turned on, print the EAPIUser details
			if($this->apicore['__APIDebug'] == "user"){
				
				print "<pre>".__CLASS__.":".__LINE__."<br/>";print_r($this->EAPIUser);print "</pre>";
				
			}
			
		//catch any exceptions thrown	
		}catch(EAPIException $e){
			
			//print the exception and return false to stop any methods
			print $e;
			return false;
			
		}

	}
	public function __Destruct(){}

	public function getUser(){
		return $this->EAPIUser['__User'];
	}

	public function getUserID(){
		return $this->EAPIUser['__UserID'];
	}

	public function getAPIUser(){
		return $this->EAPIUser['__APIUser'];
	}

	public function getAPIKey(){
		return $this->EAPIUser['__APIKey'];
	}

	public function getDebug(){
		return $this->EAPIUser['__Debug'];
	}

};

?>